﻿/*
 * Copyright © 2025−2026 WEI.ZHOU (Willis) (Chengdu). All rights reserved.
 *
 * THIS SOURCE CODE IS PROPRIETARY AND CONFIDENTIAL.
 * It is the exclusive property of WEI.ZHOU (Willis) and may not be
 * used, copied, distributed, or disclosed outside of WEI.ZHOU (Willis)
 * without express written permission.
 *
 * This code is provided to WEI.ZHOU (Willis) under a limited, 
 * non-transferable internal license, and is for use only with the author's 
 * approved purposes. No external license is granted or implied.
 *
 * Any unauthorized use, reproduction, or distribution is strictly prohibited
 * and may result in severe civil and criminal penalties.
 */
 
using System.Reflection;

namespace Aore.Utility.Extensions;

/// <summary>
///   List集合扩展方法
/// </summary>
public static class ListExtensions
{
  /// <summary>
  ///   List集合如果Count等于零，则添加一个默认类型的Item，避免包含IN关键字SQL执行异常
  /// </summary>
  /// <typeparam name="T">List类型（String，int）</typeparam>
  /// <param name="data"></param>
  /// <returns></returns>
  /// <exception cref="NullReferenceException"></exception>
  /// <exception cref="FormatException"></exception>
  public static List<T> IsEmptyAddDefalutItem<T>(this List<T> data)
  {
    if (data == null)
    {
      throw new NullReferenceException("The operation object cannot be NULL. Please check the collection.");
    }

    try
    {
      Type type = typeof(T);
      if (data.Count == 0 || !data.Any())
      {
        if (type == typeof(string))
        {
          data.Add((T)Convert.ChangeType(string.Empty, typeof(T)));
          return data;
        }

        if (type == typeof(int))
        {
          data.Add((T)Convert.ChangeType(default(T), typeof(T)));
          return data;
        }

        if (type.IsClass)
        {
          throw new Exception("SQL IN parameter collections do not support object collections");
        }
      }

      return data;
    }
    catch (Exception e)
    {
      throw new FormatException("Parameter collection type does not match. Please check collection type.", e);
    }
  }

  /// <summary>
  ///   Dapper param 预处理，修改List Count等于零的情况
  /// </summary>
  /// <param name="param"></param>
  public static void ParamIsListEmpty(ref object param)
  {
    {
      PropertyInfo[] properties = param.GetType().GetProperties();
      foreach (PropertyInfo property in properties)
      {
        object item = property.GetValue(param);
        Type type = item.GetType();
        if (type == typeof(List<string>))
        {
          List<string> list = (List<string>)item;
          list.IsEmptyAddDefalutItem();
          continue;
        }

        if (type == typeof(List<int>))
        {
          List<int> list = (List<int>)item;
          list.IsEmptyAddDefalutItem();
        }
      }
    }
  }
}